﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using WebService.Nafania.ViewLayer.Model;

namespace UI.Nafania.Controllers.Base
{
	[HandleError]
	public class BaseController : AsyncController
	{
		protected string PublicKey { get { return "50037FFE86ED4CFB"; } }

		private GeneratorServiceClient _proxy;
		protected GeneratorServiceClient Proxy { get { return _proxy ?? (_proxy = new GeneratorServiceClient("IGeneratorService")); }}

		public KeyValuePair[] RequestParams
		{
			get
			{
				KeyValuePair[] postParams = new KeyValuePair[Request.Params.AllKeys.Length];

				int i = 0;
				foreach (var key in Request.Params.AllKeys)
				{
					postParams[i] = new KeyValuePair {Key = key, Value = Request.Params[key]};
					++i;
				}

				return postParams;
			}
		}

		protected override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			if (_proxy != null)
			{
				_proxy.Close();
				((IDisposable)_proxy).Dispose();
				_proxy = null;
			}

			base.OnActionExecuted(filterContext);
		}
	}
}